"use strict";
//获取应用实例
var app = getApp()
var common = require('../../../utils/api.js')
var util = require('../../../utils/util.js')
var api = common.api

Page({
  data: {
    userInfo: {},
    manageslist:[],
    teamleaderslist:[],
    scrollViewHeight: 200,
    productList:null,
    existuser:true,
    page:'index',
    cannotFetch:false
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    console.log("onLoad")
  },
  onShow:function(){
    console.log("onShow")
    this.setData({existuser:true,cannotFetch:false})
    app.globalData.userInfo = null
        util.showLoading()
    var res = wx.getSystemInfoSync()
    var that = this
    console.log(app,res.windowHeight,res.windowHeight*res.pixelRatio)    
    this.setData({scrollViewHeight:res.windowHeight*res.pixelRatio})
    // console.log(this.scrollViewHeight)
    app.getUserInfo(function(){
      that.getUserInfo()
    });
  },
  getUserInfo: function(){
    var that = this
    var openid = app.globalData.wxuserInfo.openid
     api.getuserinfo(openid,function(data){
       if(data.cannotFetch===true){
         //外网用户无法访问
         that.setData({cannotFetch:true})
         util.hideLoading()
         return false;
       }
      var userinfo = data.data
      if(!userinfo || util.isEmpty(userinfo)){
        console.log('用户不存在')
        that.setData({existuser:false})
        util.hideLoading()
        return false;
      }
       console.log(userinfo);
      that.getTeamleadersWithManagelistuserlist()
      if(userinfo.ispmo){
        app.globalData.pmo = userinfo.name
      }
      console.log("用户信息",userinfo)
      app.globalData.userInfo = userinfo
      
      that.setData({userInfo:userinfo})
      that.getactiveList(userinfo.id)
    })
  },
  onGotUserInfo: function(){

  },
  canScore:function(id,result){
    result.forEach(function(item){
      var temp = item.teamleader.split(',').map(function(item2){
        return parseInt(item2,10)
      })
      item.canScore = temp.indexOf(id)!=-1
    })
    
  },
  getactiveList:function(id){
    var that = this
    //调用应用实例的方法获取全局数据
    api.getactiveList(id,function(result){
      if(result){
      //更新数据
        that.mapData(result)
      }
      console.log('首页数据',result)
      that.canScore(that.data.userInfo.id,result)
      that.setData({
        productList:result
      })
      util.hideLoading()
    })
  },
  getTeamleadersWithManagelistuserlist:function(){
    var that = this
    //调用应用实例的方法获取全局数据
    api.getuserlist(function(result){
      console.log('设置全局的项目经理名单，技术经理名单',result)
      //更新数据
      app.manageslist=result.manages
      app.teamleaderslist=result.teamleaders
    })
  },
  mapData:function(result){
    result.forEach(function(item){
      if(item.status==1){
       var flag = util.checktime(item.productdate)
        if(flag>0){//进行中 
          item.statusinfo = util.productstatus[1].name
        }else{
          item.statusinfo = util.productstatus[0].name
        }
      }else{
        item.statusinfo = util.productstatus[item.status-1].name
      }
    })
  },
  upper: function(e) {
    console.log(e)
  },
  lower: function(e) {
    console.log(e)
  },
  scroll: function(e) {
    //console.log(e)
  }
})
